package qf.wlh.com.oneprisdent.speakfragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import qf.wlh.com.oneprisdent.R;
import qf.wlh.com.oneprisdent.adapter.DateAdapter;

/**
 * A simple {@link Fragment} subclass.
 */
public class DateFragment extends Fragment {
    private ViewPager viewPager;
    private LinearLayout linearLayout;
    private HorizontalScrollView horizontalScrollView;
    //存储数据的集合
    private List<String>list=new ArrayList<>();
    //上面的导航字体日期
    private List<TextView> textViews =new ArrayList<>();
    private DateAdapter adapter;
    private List datas=new ArrayList();
    public DateFragment() {
        // Required empty public constructor
        for (int i = 0; i < 15; i++) {
            list.add("我是吴立辉"+i);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.datelayout, container, false);
        viewPager= (ViewPager) view.findViewById(R.id.dateviewpager);
        linearLayout= (LinearLayout) view.findViewById(R.id.linearlayout);
        horizontalScrollView= (HorizontalScrollView) view.findViewById(R.id.scrollview);
        initData();
        return view;
    }


    private void initData() {

        //LinearLayout中添加TextView
        for(int i=0;i<list.size();i++){
            TextView textView = new TextView(getContext());
            textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            textView.setText(list.get(i));
            textView.setTextColor(Color.WHITE);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
            textView.setPadding(10, 7, 10, 7);
            textView.setTag(i);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position= (int) view.getTag();
                    viewPager.setCurrentItem(position);
                }
            });
            textViews.add(textView);
            linearLayout.addView(textView);
        }

        //把第一TextView设置为选中状态
        textViews.get(0).setEnabled(false);

        adapter=new DateAdapter(datas,getContext(),list);
        viewPager.setAdapter(adapter);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //先全部设置为可以点击
                for (TextView textView :textViews) {
                    textView.setEnabled(true);
                }
                //把最后选中的设置为不可点击
                TextView textView  = textViews.get(position);

                textView.setEnabled(false);

                int left=textView.getLeft();    //控件左侧的位置
                int width=textView.getWidth();   //控件宽度
                int widthPixels  = getResources().getDisplayMetrics().widthPixels; //屏幕的宽度

                int dx=(widthPixels/2-left-width/2);
                //水平的滚动视图要挪动
                //负 往右
                //正 往左
                horizontalScrollView.smoothScrollTo(dx*-1,0);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

}
